package net.maku.module.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.security.user.SecurityUser;
import net.maku.module.entity.AgIndustryInnovation;
import net.maku.module.service.AgIndustryInnovationService;
import net.maku.module.vo.AgIndustryInnovationVO;
import net.maku.system.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/industryInnovation")
@Tag(name = "业务板块")
@AllArgsConstructor
public class AgIndustryInnovationController {

    private final AgIndustryInnovationService agIndustryInnovationService;
    private final SysUserService sysUserService;

    @GetMapping("/page")
    @Operation(summary = "分页查询")
    public Result<PageResult<AgIndustryInnovationVO>> page(@RequestParam(defaultValue = "1") Integer pageNum,
                                                           @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "")Integer websiteId, @RequestParam(defaultValue = "")Long tenantId) {
        Page<AgIndustryInnovation> AgIndustryInnovationPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AgIndustryInnovation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(websiteId), AgIndustryInnovation::getWebsiteId, websiteId)
                .eq(ObjectUtil.isNotNull(tenantId), AgIndustryInnovation::getTenantId, tenantId);
        IPage<AgIndustryInnovation> page = agIndustryInnovationService.page(AgIndustryInnovationPage,wrapper);
        List<AgIndustryInnovationVO> collect = page.getRecords().stream().map(bean -> {
            AgIndustryInnovationVO vo = new AgIndustryInnovationVO();
            BeanUtil.copyProperties(bean, vo);
            if (ObjectUtil.isNotNull(bean.getCreator())) {
                vo.setCreatedByName(sysUserService.getById(bean.getCreator()).getUsername());
            }
            if (ObjectUtil.isNotNull(bean.getUpdator())) {
                vo.setUpdatedByName(sysUserService.getById(bean.getUpdator()).getUsername());
            }
            return vo;
        }).collect(Collectors.toList());
        return Result.ok(new PageResult<>(collect, page.getTotal()));
    }

    @GetMapping("/list")
    @Operation(summary = "查询列表")
    public Result<List<AgIndustryInnovation>> list(@RequestParam(defaultValue = "")Integer websiteId,@RequestParam(defaultValue = "")Long tenantId) {
        LambdaQueryWrapper<AgIndustryInnovation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(websiteId), AgIndustryInnovation::getWebsiteId, websiteId)
                .eq(ObjectUtil.isNotNull(tenantId), AgIndustryInnovation::getTenantId, tenantId);
        List<AgIndustryInnovation> list = agIndustryInnovationService.list(wrapper);
        return Result.ok(list);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除板块信息")
    public Result<String> delete(@RequestBody List<Long> ids) {
        agIndustryInnovationService.removeBatchByIds(ids);
        return Result.ok();
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "保存||更新")
    public Result<String> saveOrUpdate(@RequestBody AgIndustryInnovation agIndustryInnovation) {
        if (null == agIndustryInnovation){
            return Result.error("参数错误");
        }
        if (ObjectUtil.isNotNull(agIndustryInnovation.getId())){
            agIndustryInnovation.setUpdator(SecurityUser.getUserId());
            agIndustryInnovation.setUpdateTime(LocalDateTime.now());
        }else {
            agIndustryInnovation.setCreateTime(LocalDateTime.now());
            agIndustryInnovation.setCreator(SecurityUser.getUserId());
        }
        agIndustryInnovationService.saveOrUpdate(agIndustryInnovation);
        return Result.ok();
    }

    @GetMapping("/detail")
    @Operation(summary = "详情")
    public Result<AgIndustryInnovation> detail(@RequestParam Long id) {
        AgIndustryInnovation AgIndustryInnovation = agIndustryInnovationService.getById(id);
        return Result.ok(AgIndustryInnovation);
    }

}
